package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.SubjectLessonDto;
import com.dingreading.cloud.admin.dto.SubjectLessonListDto;
import com.dingreading.cloud.admin.entity.SubjectLesson;
import com.dingreading.cloud.admin.entity.SubjectLessonVideo;
import com.dingreading.cloud.admin.entity.Subjects;
import com.dingreading.cloud.admin.service.SubjectLessonService;
import com.dingreading.cloud.admin.service.SubjectsService;
import com.dingreading.cloud.admin.util.CdnUrlUtil;
import com.dingreading.cloud.admin.util.bean.SubjectUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/subjectLesson")
@Api(tags = "学科课次管理")
public class SubjectLessonController {

    @Resource
    private SubjectsService subjectsService;
    @Resource
    private SubjectLessonService subjectLessonService;
    @Resource
    private CdnUrlUtil cdnUrlUtil;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "主学科id") @RequestParam(value = "subjectMainId", required = false) Long subjectMainId,
            @ApiParam(value = "关联学科的id") @RequestParam(value = "subjectId") Long subjectId
    ) {
        Page<SubjectLessonListDto> pageList = subjectLessonService.pageList(pageUtil, subjectMainId, subjectId);
        long total = pageList.getTotalRow();
        List<SubjectLessonListDto> list = pageList.getRecords();

        PageDto<SubjectLessonListDto> data = new PageDto<>();
        data.setTotal(total);
        data.setList(list);
        return R.ok(data);
    }


    @ApiOperation(value = "更新课次启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课次id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        subjectLessonService.updateEnabled(id, enabled);
        return R.ok();
    }


    @ApiOperation(value = "学科批量创建课次")
    @PostMapping(value = "/newLessons")
    public R<Object> newLessons(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "学科id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "课次数量") @RequestParam(value = "lessonNum", required = false) Integer lessonNum
    ) {
        Subjects data = subjectsService.getById(id);
        if (data == null) return R.fail("学科获取失败");
        if (!data.getLevelEnd().equals(1)) return R.fail("不是最后一级，不能创建课次");

        Subjects mainSubjects = subjectsService.getById(data.getSubjectMainId());
        if (mainSubjects == null) return R.fail("主学科获取失败");

        Subjects parentSubjects = subjectsService.getById(data.getParentId());
        if (parentSubjects == null) return R.fail("上级学科获取失败");

        // 获取学科下课次的最大序号
        int maxLessonOrder = subjectLessonService.getMaxLessonOrder(mainSubjects.getId(), id);

        // 创建课次
        List<SubjectLesson> newList = new ArrayList<>();
        for (int i = 1; i <= lessonNum; i++) {
            SubjectLesson newLesson = SubjectUtil.newLesson(
                    mainSubjects.getId(), mainSubjects.getSubjectName(), data.getId(), maxLessonOrder + i, data.getNodes()
            );
            newList.add(newLesson);
        }
        if (CollectionUtils.isNotEmpty(newList))
            subjectLessonService.saveOrUpdateBatch(newList);

        // 更新学科的课次数量
        subjectsService.updateLessonNum(id, data.getLessonNum() + lessonNum);

        return R.ok();
    }


    @ApiOperation(value = "获取课次信息")
    @PostMapping(value = "/get")
    public R<Object> get(
            HttpServletRequest request,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid
    ) {
        SubjectLessonDto data = subjectLessonService.dtoByUid(uid);
        if (data == null)
            return R.failGet();
        if (StringUtils.isNotBlank(data.getBookImg())) {
            R<String> r = cdnUrlUtil.getCdnUrl(request, data.getBookImg());
            if (R.isError(r))
                return R.fail(r.getMsg());
            data.setBookImgCdnUrl(r.getData());
        }
        return R.ok(data);
    }


    @ApiOperation(value = "更新书本信息")
    @PostMapping(value = "/updateBookInfo")
    public R<Object> updateBookInfo(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课次uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "书本isbn") @RequestParam(value = "bookIsbn") String bookIsbn,
            @ApiParam(value = "书本出版社") @RequestParam(value = "bookPress") String bookPress,
            @ApiParam(value = "书本封面图") @RequestParam(value = "bookImg") String bookImg
    ) {
        return subjectLessonService.updateBookInfo(headStaffUid, headStaffName, uid, bookIsbn, bookPress, bookImg);
    }


    @ApiOperation(value = "添加访问记录")
    @PostMapping(value = "/addRecord")
    public R<Object> addRecord(
            @ApiParam(value = "开始日期 yyyy-MM-dd") @RequestParam(value = "beginDate") String beginDate,
            @ApiParam(value = "结束日期 yyyy-MM-dd") @RequestParam(value = "endDate") String endDate
    ) {
        return subjectLessonService.addRecord(beginDate, endDate);
    }

}